import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';

@Component({
  selector: 'upgrade-select',
  templateUrl: './upgrade-select.component.html',
  styles: []
})
export class UpgradeSelectComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    let index=this.param[0].deviceType;
    this.submitDeviceType=index;
    this.deviceType=index;
    // switch(index){
    //   case "6":
    //       this.deviceType="1";
    //       break;
    //   case "2":
    //     this.deviceType="2";
    //     break;
    // }
  };
  param: any;
  deviceType:string;
  submitDeviceType:string;
  show:boolean=false;
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'upgradeName',
        title: '包名',
        filter: {
          placeholder: '请输入包名',
          type: 'string'
        }
      },
      {
        index: 'upgradeSize',
        title: '大小'
      },
      {
        index:'upgradeType',
        title:'包类型',
        type:ColumnType.tag,
        tag:{
          1:{text:'终端升级程序'},
          2:{text:'固件升级程序'},
          3:{text:'MCU固件'},
          4:{text:'读头固件'},
          5:{text:'IPC固件'},
          6:{text:'单片机升级程序'},
        },
        filter:{
          type:'select',
          placeholder:"请选择包类型",
          items:[
            {label: '终端升级程序', value: 1},
            {label: '固件升级程序', value: 2},
            {label: 'MCU固件',value:3},
            {label: '读头固件',value:4},
            {label: 'IPC固件',value:5},
            {label: '单片机升级程序', value: 6},
          ]
        }
      },
      {
        index:'upgradeWay',
        title:'设备类型',
        type:ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'},
          7:{text: '门铃'},
          8:{text: '4G开门控制器'},
          12:{text: 'linux人脸机'}
        },
        filter:{
          type:'select',
          placeholder:"请选择设备类型",
          items:[
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
          ]
        }
      },
      {
        index: 'upgradeVersion',
        title: '版本号',
        filter: {
          placeholder: '请输入版本号',
          type: 'string'
        }
      },
      {
        index: 'updateTime',
        title: "上传时间",
        type:ColumnType.date
      }
    ],
    showCheckBox: false,
    //reqUrl:'/core/upgrade/page',
    data:[],
    editBtn: [
      {title: '选择'}
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService,private ref: NzModalRef, private modal: NzModalService, private message: NzMessageService) { }
  //this.param.id
  ngOnInit() {
    this.getTableData();
    
  }
  getTableData(){
    this.http.post('/core/upgrade/page',{}).subscribe(res=>{
      console.log(res)
      if(res.status == 200){
        this.config.data=res.data.list.filter(item=>{
          return item.upgradeWay == this.deviceType;
        });
        this.show=true;
      }
    })
  }

  operate(e: any): void {
    console.log(e.data);
    if (e.title == '选择') {
      this.updateAll(e.data);
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  dataProcess(data: STData[]){
    console.log(data);
  //  let arr=data.filter(item=>{
  //    return item.upgradeWay == this.deviceType;
  //  })
  //  console.log(arr)
  
    return data;
  }
  //升级设备
  updateAll(data:any) {
    
    this.modal.confirm({
      nzTitle: '您确定要升级这些设备吗',
      nzOnOk: () => {
        const body = [];
        this.param.forEach(element => {
          body.push({deviceCode:element.deviceCode,version:data.upgradeVersion,download_url:data.upgradeUrl,fileName:data.upgradeName,upgradeType:data.upgradeType})
        });
        this.http.post('/core/upgrade/upgradeHost',{deviceType:this.submitDeviceType,upgradeHostDto:body}).subscribe(res => {
          if(res.status === '200'){
            this.message.success('升级成功');
            this.ref.triggerOk();
          }
        })
      }
    })
    
  }
  

}
